package cn.ucox.web.ms.controller;

import cn.ucox.web.ms.domain.ApiPager;
import cn.ucox.web.ms.domain.ApiQueryBase;
import cn.ucox.web.ms.polling.SyncAnchor;
import cn.ucox.web.ms.polling.SyncRedisUtil;
import cn.ucox.web.ms.service.IMonitorService;
import cn.ucox.web.ms.view.ApiResult;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Tuple;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by Administrator on 2017/9/4.
 */
@Controller
@RequestMapping(value = "monitor")
public class MonitorController {

    @Resource
    private IMonitorService monitorService;

    /**
     * 同步锚点监控
     * SYNC:ANCHOR
     *
     * @return
     */
    @RequestMapping(value = "sync-anchor", method = GET)
    @ResponseBody
    public ApiResult getSyncAnchor(){

        SyncAnchor anchor = monitorService.getSyncAnchor();
        return new ApiResult<SyncAnchor>().success("同步锚点监控",anchor);
    }

    /**
     * 解析锚点监控
     * FTP:DATA:PARSE:ANCHOR
     *
     * @return
     */
    @RequestMapping(value = "parse-anchor", method = GET)
    @ResponseBody
    public ApiResult getParseAnchor(){

        Map<String, String> anchor = monitorService.getParseAnchor();
        return new ApiResult<Map>().success("解析锚点监控",anchor);
    }

    /**
     * 异常数据监控
     * LOG:PARSE
     *
     * @return
     */
    @RequestMapping(value = "log-parse", method = GET)
    @ResponseBody
    public ApiResult getLogParse(ApiQueryBase apiQueryBase){


        return monitorService.getLogParse(apiQueryBase);

    }

    /**
     * 异常数据删除
     * LOG:PARSE
     *
     * @return
     */
    @RequestMapping(value = "del-log-parse", method = POST)
    @ResponseBody
    public ApiResult remLogParse(HttpServletRequest request){

        String value = request.getParameter("value");
        if(StringUtils.isNotBlank(value)){
            return monitorService.remLogParse(value);
        }
        return new ApiResult<>().failure("所需删除value为空");
    }


    /**
     * 异常备份数据监控
     * SYNC:TRANS:RESULT:DATA:BACKUP
     *
     * @param apiQueryBase
     * @return
     */
    @RequestMapping(value = "data-backup", method = GET)
    @ResponseBody
    public ApiResult getDataBackup(ApiQueryBase apiQueryBase){


        return monitorService.getDataBackup(apiQueryBase);

    }

    /**
     * 队列数据监控
     * SYNC:FTP:DATA:ANCHOR:事务号
     *
     * @return
     */
    @RequestMapping(value = "data-anchor", method = GET)
    @ResponseBody
    public ApiResult getDataAnchor(){

        Set<String> anchor = monitorService.getDataAnchor();
        return new ApiResult<Set<String>>().success("队列数据监控",anchor);
    }

    /**
     * 队列数据监控详情
     * SYNC:FTP:DATA:ANCHOR:事务号
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "data-anchor-detail", method = GET)
    @ResponseBody
    public ApiResult getDataAnchorDetail(@RequestParam("key") String key){

        Map<String, String> anchor = monitorService.getDataAnchorDetail(key);
        return new ApiResult<Map>().success("队列数据监控详情",anchor);
    }

    /**
     * 吞吐率监控
     * SYNC:MONITOR:FTP
     *
     * @return
     */
    @RequestMapping(value = "monitor-ftp", method = GET)
    @ResponseBody
    public ApiResult getMonitorFtp(HttpServletRequest request){

        String interval = request.getParameter("interval");
        int second = 300;
        if(StringUtils.isNotBlank(interval)){
            second = Integer.valueOf(interval);
        }

        JSONObject anchor = monitorService.getMonitorFtp(second);
        return new ApiResult<JSONObject>().success("队列数据监控详情",anchor);
    }

    /**
     * SYNC:FTP:FILES
     * Redis文件索引表
     */
    @RequestMapping(value = "ftp-files", method = GET)
    @ResponseBody
    public ApiResult getFtpFiles(){

        Set<Tuple> anchor = monitorService.getFtpFiles();
        return new ApiResult<Set>().success("Redis文件索引表",anchor);
    }

    /**
     * SYNC:QUEUE:BACKUP
     * FTP:READY:TRANS:DATA
     * SYNC:QUEUE
     *
     */

    /**
     * SYNC:LOG:FTP
     * 事务分片、上传FTP异常
     */
    @RequestMapping(value = "slicer-ftp", method = GET)
    @ResponseBody
    public ApiResult getSlicerFtp(){

        Set<Tuple> anchor = monitorService.getSlicerFtp();
        return new ApiResult<Set>().success("事务分片、上传FTP异常列表",anchor);
    }

    /**
     * FTP:TRANS:DATA:TIMER
     * 事务数据激活时间时间(事务中每个分片数据均会激活一次)
     * 如果数据已完整，则从计时器中删除
     */
    @RequestMapping(value = "trans-timer", method = GET)
    @ResponseBody
    public ApiResult getTransTimer(){

        Map<String, String> anchor = monitorService.getTransTimer();
        return new ApiResult<Map>().success("事务数据激活时间",anchor);
    }

}
